package com.lineage.server.model;

import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.serverpackets.ServerBasePacket;
import com.lineage.server.world.WorldClan;
import java.util.ArrayList;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public final class L1Alliance {
  private static final Log _log = LogFactory.getLog(L1Alliance.class);
  
  private static final int MAX_SIZE = 4;
  
  private final int _orderId;
  
  private final ArrayList<L1Clan> totalList;
  
  public L1Alliance(int orderId, L1Clan... clanList) {
    this._orderId = orderId;
    this.totalList = new ArrayList<>(4);
    byte b;
    int i;
    L1Clan[] arrayOfL1Clan;
    for (i = (arrayOfL1Clan = clanList).length, b = 0; b < i; ) {
      L1Clan alliance = arrayOfL1Clan[b];
      this.totalList.add(alliance);
      b++;
    } 
  }
  
  public int getOrderId() {
    return this._orderId;
  }
  
  public final ArrayList<L1Clan> getTotalList() {
    return this.totalList;
  }
  
  public final boolean addAlliance(L1Clan l1clan) {
    if (checkSize())
      return this.totalList.add(l1clan); 
    return false;
  }
  
  public final boolean checkSize() {
    return (this.totalList.size() < 4);
  }
  
  public void sendPacketsAll(String excludingPcName, ServerBasePacket packet) {
    try {
      for (L1Clan clan : this.totalList) {
        if (clan == null || WorldClan.get().getClan(clan.getClanName()) == null)
          continue; 
        byte b;
        int i;
        L1PcInstance[] arrayOfL1PcInstance;
        for (i = (arrayOfL1PcInstance = clan.getOnlineClanMember()).length, b = 0; b < i; ) {
          L1PcInstance listner = arrayOfL1PcInstance[b];
          if (!listner.getExcludingList().contains(excludingPcName))
            listner.sendPackets(packet); 
          b++;
        } 
      } 
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } 
  }
}
